/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rbh.risk.dao.impl;

import com.rbh.risk.dao.SubEventDao;
import com.rbh.risk.entity.MainEvent;
import com.rbh.risk.entity.SubEvent;
import com.rbh.risk.support.SessionFactorySupport;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

@Repository("subEventDao")
public class SubEventDaoImpl extends SessionFactorySupport implements SubEventDao {

    @Override
    public void insert(SubEvent subEvent) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().save(subEvent);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public void update(SubEvent subEvent) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().update(subEvent);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public void delete(SubEvent subEvent) throws DataAccessException {
        try {
            super.getSessionFactory().getCurrentSession().delete(subEvent);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public SubEvent getById(Integer subEventId) throws DataAccessException {
        SubEvent result = null;
        try {
            result = (SubEvent) super.getSessionFactory().getCurrentSession().get(SubEvent.class, subEventId.shortValue());
        } catch (Exception ex) {
            throw ex;
        }
        return result;
    }

    @Override
    public List<SubEvent> getAll() throws DataAccessException {
        List<SubEvent> results = null;
        try {
            Query query = super.getSessionFactory().getCurrentSession().createQuery("from " + SubEvent.class.getName());
            results = query.list();
        } catch (Exception ex) {
            throw ex;
        }
        return results;
    }

    @Override
    public List<SubEvent> getByWords(String words) throws DataAccessException {
        List<SubEvent> results = null;
        try {
            Criteria criteria = super.getSessionFactory().getCurrentSession().createCriteria(SubEvent.class);
            criteria.add(Restrictions.like("subEventName", words, MatchMode.ANYWHERE));
            results = criteria.list();
        } catch(Exception ex){
            throw ex;
        }
        return results;
    }

    @Override
    public List<SubEvent> getByMainEvent(MainEvent mainEvent) throws DataAccessException {
        List<SubEvent> results = null;
        try {
            Criteria criteria = super.getSessionFactory().getCurrentSession().createCriteria(SubEvent.class);
            criteria.add(Restrictions.disjunction()
                    .add(Restrictions.eq("mainEvent", mainEvent))
                    .add(Restrictions.isNull("mainEvent")));
            results = criteria.list();
        }catch(Exception ex){
            throw ex;
        }
        return results;
    }
}
